<?php
/**
 * 陈杰  18323491246
 */

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Api\ExtendChatModel;
use App\Models\Api\ExtendGroupModel;
use App\Models\Api\ExtendUserModel;
use Illuminate\Http\Request;

class ApiIndexController extends Controller
{

    public function group_index(Request $request)
    {
        $gid = (int)$request->input('gid');
        if ($gid < 1)
            return self::errorMsg('请输入正确的群组id');
    }

    public function return_me(Request $request)
    {
        $gid = (int)$request->input('gid');
        if ($gid < 1)
            return self::errorMsg('请输入正确的群组id');
        $count = ExtendChatModel::query()->where('gid', $gid)
            ->where('date', '>', $request->get('user')['out_time'])
            ->where('nid', $request->get('uid'))
            ->count();
        return self::returnMsg($count);
    }

    public function chat_list(Request $request)
    {
        $gid = (int)$request->input('gid');
        if ($gid < 1)
            return self::errorMsg('请输入正确的群组id');
        $list = ExtendChatModel::query()->where('gid', $gid)
            ->offset($request->get('page'))
            ->limit($request->get('count'))
            ->orderByDesc('id')
            ->get()->toArray();
        foreach ($list as $v) {
            $data[] = self::createBox($v);
        }
        return self::returnMsg($data);
    }

    public function box_list(Request $request)
    {
        $group = ExtendUserModel::get_group($request->get('uid'));
        if (!isset($group))
            return self::errorMsg();

        foreach ($group as $v) {
            $list[$v]['info'] = ExtendGroupModel::get_by_id($v);
            $box = ExtendChatModel::get_list($request->get('page'), $request->get('count'), $v);
            foreach ($box as $k => $p) {
                $boxList[] = self::createBox($p);
            }
            $list[$v]['list'] = $boxList;
        }
        return self::returnMsg($list);
    }

}
